{{- $img := false -}}
{{- $width := false -}}
{{- $height := false -}}
{{- $pageResource := false -}}
{{- $link := "" -}}
{{- $resizable := false -}}
{{- $unresizable := slice ".svg" ".avif" -}}
{{- $url := urls.Parse .filename -}}
{{- $static := false -}}
{{- $ext := "" -}}
{{- $lazyLoading := default true .lazyLoading -}}
{{- if not $url.Scheme -}}
  {{- $ext = $url.Path | path.Ext | lower -}}
  {{- $resource := .resources.GetMatch (strings.TrimPrefix "/" $url.Path) -}}
  {{- if $resource -}}
      {{- $pageResource = $resource -}}
      {{- $link = $resource.RelPermalink -}}
      {{- if not (in $unresizable $ext) -}}
        {{- $resizable = true -}}
      {{- end -}}
  {{- else -}}
    {{- $static = true -}}
    {{- $filename := path.Join "static" $url.Path -}}
    {{- if fileExists $filename -}}
      {{- $hash := "" }}
      {{- with os.Stat $filename }}
        {{- $hash = .ModTime.Format "20060102150405" | md5 }}
      {{- end }}
      {{- if $hash }}
       {{- $link = printf "%s?v=%s" .filename $hash | absURL }}
      {{- else }}
        {{- $link = .filename | absURL }}
      {{- end }}
      {{- if not (in $unresizable $ext) -}}
        {{- $img = imageConfig $filename -}}
      {{- end -}}
    {{- else -}}
      {{- warnf "no such image can be found in page resources and the static folder: %s" .filename -}}
    {{- end -}}
  {{- end -}}
{{- else -}}
  {{- $link = .filename -}}
{{- end -}}
{{- $params := $url.Query -}}
{{- if index $params "width" -}}
  {{- $width = index $params.width 0 -}}
{{- end -}}
{{- if index $params "height" -}}
  {{- $height = index $params.height 0 -}}
{{- end -}}
{{- $className := default "img-fluid" .class -}}
{{- $posClassName := "" -}}
{{- if eq $url.Fragment "center" -}}
  {{- $posClassName = "mx-auto d-block" -}}
{{- else if or (eq $url.Fragment "floatleft") (eq $url.Fragment "float-start") -}}
  {{- if eq $url.Fragment "floatleft" }}
    {{- warnf "The floatleft fragment is deprecated, will be removed in the future, please use float-start instead." }}
  {{- end }}
  {{- $posClassName = "float-start me-2" -}}
{{- else if or (eq $url.Fragment "floatright") (eq $url.Fragment "float-end") -}}
  {{- if eq $url.Fragment "floatright" }}
    {{- warnf "The floatright fragment is deprecated, will be removed in the future, please use float-end instead." }}
  {{- end }}
  {{- $posClassName = "float-end ms-2" -}}
{{- end -}}

{{- if and $pageResource $resizable $img }}
{{ $img  = $img.Filter (images.Overlay $img 50 50 )}}
{{- end -}}

{{- if and $pageResource $resizable -}}
  {{- $img = $pageResource -}}
  {{- if or $width $height }}
    {{- $resize := "" -}}
    {{- if and $width $height -}}
      <!-- Resize to actual entered size -->
      {{- $resize = (printf "%sx%s" (strings.TrimSuffix "px" $width) (strings.TrimSuffix "px" $height)) -}}
    {{- else if $width }}
      <!-- Resize width to entered size, keeping aspect ratio -->
      {{- $resize = (printf "%sx" (strings.TrimSuffix "px" $width)) -}}
    {{- else }}
      <!-- Resize height to entered size, keeping aspect ratio -->
      {{- $resize = (printf "x%s" (strings.TrimSuffix "px" $height)) -}}
    {{- end -}}
    {{- $img = $img.Resize $resize -}}
  {{- end }}
  {{- with $params.crop }}
    {{- $opts := delimit (split (index . 0) ",") " " | string -}}
    {{- $img = $img.Crop $opts }}
  {{- end }}
  {{- with $params.fill }}
    {{- $opts := delimit (split (index . 0) ",") " " | string -}}
    {{- $img = $img.Fill $opts }}
  {{- end }}
  {{- with $params.fit }}{{ $img = $img.Fit (index . 0) }}{{ end }}
  {{- $filters := slice }}
  {{- with $params.brightness }}{{ $filters = $filters | append (images.Brightness (index . 0)) }}{{ end }}
  {{- with $params.gaussianBlur }}{{ $filters = $filters | append (images.GaussianBlur (index . 0)) }}{{ end }}
  {{- with $params.pixelate }}{{ $filters = $filters | append (images.Pixelate (index . 0)) }}{{ end }}
  {{- with $params.contrast }}{{ $filters = $filters | append (images.Contrast (index . 0)) }}{{ end }}
  {{- with $params.gamma }}{{ $filters = $filters | append (images.Gamma (index . 0)) }}{{ end }}
  {{- with $params.grayscale }}{{ $filters = $filters | append images.Grayscale }}{{ end }}
  {{- with $params.hue }}{{ $filters = $filters | append (images.Hue (index . 0)) }}{{ end }}
  {{- with $params.invert }}{{ $filters = $filters | append images.Invert }}{{ end }}
  {{- with $params.saturation }}{{ $filters = $filters | append (images.Saturation (index . 0)) }}{{ end }}
  {{- with $params.sepia }}{{ $filters = $filters | append (images.Sepia (index . 0)) }}{{ end }}
  {{- with $params.sigmoid }}
    {{- $balance := split (index . 0) "," -}}
    {{- $filters = $filters | append (images.Sigmoid (index $balance 0) (index $balance 1)) }}
  {{- end }}
  {{- with $params.unsharpMask }}
    {{- $balance := split (index . 0) "," -}}
    {{- $filters = $filters | append (images.UnsharpMask (index $balance 0) (index $balance 1) (index $balance 2)) }}
  {{- end }}
  {{- with $params.colorBalance }}
    {{- $balance := split (index . 0) "," -}}
    {{- $filters = $filters | append (images.ColorBalance (index $balance 0) (index $balance 1) (index $balance 2)) }}
  {{- end }}
  {{- with $params.colorize }}
    {{- $balance := split (index . 0) "," -}}
    {{- $filters = $filters | append (images.Colorize (index $balance 0) (index $balance 1) (index $balance 2)) }}
  {{- end }}
  {{- with $filters }}
    {{- $img = $img | images.Filter $filters }}
  {{- end }}
  {{- $link = $img.RelPermalink -}}
{{- end -}}

{{- $alternativeFormats := slice "avif" "webp" "jxl" -}}

{{- $caption := and (default false .page.Site.Params.post.imageTitleAsCaption) .title }}

{{- if $caption -}}
{{- $className = printf "%s figure-img" $className -}}
<figure class="figure{{ with $posClassName }} {{ . }}{{ end }}">
{{- end -}}
<picture>
  {{- range $alternativeFormats -}}
    {{- $filename := replace $url.Path $ext (printf ".%s" .) -}}
    {{- if and $static (not (strings.HasSuffix $ext .)) (fileExists (path.Join "static" $filename)) -}}
      <source srcset="{{ $filename | absURL }}" type="image/{{ . }}" />
    {{- end -}}
  {{- end -}}
  <img class="{{ $className }}{{ if not $caption }} {{ $posClassName }}{{ end }}" alt="{{ .alt }}" src="{{ $link }}"
    {{- with $lazyLoading }} loading="lazy"{{ end }}
    {{- with .attributes }} {{ . }}{{ end }}
    {{- with .title }} title="{{ . }}"{{ end }}
    {{- with $img }} width="{{ .Width }}" height="{{ .Height }}"{{ end }}
    {{- if and (not (and $pageResource $resizable)) (or $width $height) }}style="{{ with $width }}width: {{ . }}; {{ end }}{{ with $height }}height: {{ . }}{{ end }}"{{ end }} />
</picture>
{{- if $caption -}}
<figcaption class="figure-caption">{{ .title }}</figcaption>
</figure>
{{- end }}
